package org.example.lock;

import cn.lingyangwl.framework.lock.annotation.DistributedLock;
import cn.lingyangwl.framework.lock.manager.ILockManager;
import cn.lingyangwl.framework.lock.model.ILock;
import cn.hutool.core.util.RandomUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author shenguangyang
 */
@Service
public class TestLock {
    @Resource
    private ILockManager lockManager;

    @Resource(name = "zkLock")
    private MyLock lock;
//    @Resource(name = "curatorLock")
//    private ILock lock;

    private int goodsStock = 100;

    public void testZkLock() throws InterruptedException {
        lock.lock();
        try {
            if (goodsStock == 0) {
                System.err.println("商品库存不足");
                return;
            }
            goodsStock = goodsStock - 1;
            System.out.println("buy goods, stock: " + goodsStock);
            TimeUnit.MILLISECONDS.sleep(RandomUtil.randomInt(1, 100));
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }
    }

    public void testCuratorLock() throws InterruptedException {
        try {
            lock.lock();
            if (goodsStock == 0) {
                System.err.println("商品库存不足");
                return;
            }
            goodsStock = goodsStock - 1;
            System.out.println("buy goods, stock: " + goodsStock);
            TimeUnit.MILLISECONDS.sleep(RandomUtil.randomInt(1, 300));

            // 再次获取锁, 可重入锁
            lock.lock();
            TimeUnit.MILLISECONDS.sleep(RandomUtil.randomInt(1, 50));
            lock.unlock();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }
    }

    public void testLockService() throws InterruptedException {
        ILock lock = lockManager.getLock("/lock");
        try {
            lock.lock();
            if (goodsStock == 0) {
                System.err.println("商品库存不足");
                return;
            }
            goodsStock = goodsStock - 1;
            System.out.println("buy goods, stock: " + goodsStock);
            TimeUnit.MILLISECONDS.sleep(RandomUtil.randomInt(1, 100));

            // 再次获取锁, 可重入锁
            lock.lock();
            TimeUnit.MILLISECONDS.sleep(RandomUtil.randomInt(1, 50));
            lock.unlock();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }
    }

    @DistributedLock
    public void testDistributedLock()  {
        try {
            if (goodsStock == 0) {
                System.err.println("商品库存不足");
                return;
            }
            goodsStock = goodsStock - 1;
            System.out.println("buy goods, stock: " + goodsStock);
            TimeUnit.MILLISECONDS.sleep(RandomUtil.randomInt(1, 100));

        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
